import { attrDefine } from '../../defines/attr_define';
import { Goods } from '../goods'

// 基本物品，实现获取、销毁、丢弃等基本功能
export class BasicGoods extends Goods{
	/**
	 * 是否可以获取
	 * @returns
	 */
	getIsGet() {
		return true;
	}

	/**
	 * 是否可以销毁物品
	 * @returns
	 */
	getIsDestory() {
		return true;
	}
	getIsDestoryConfirm(){
		return true;
	}

	getIsDiscardConfirm(){
		return true;
	}

	getControls() {
		let controls = [
			{ key: "Get", text: "获取" },
			{ key: "Destory", text: "销毁" },
			{ key: 'Discard', text: "丢弃" },
			// { key: 'Sold', text: "出售" },
		];

		return controls;
	}

	/**
	 * --------------------   控制方法   ----------------------
	 */

	// 物品判断需要什么类型的获取
	getControlGet() {
		// 如果这样物品存在于背包中，那么将其删除
		if(this.backpackId != null){
			let num = this.num;
			this.root.backdrop.consume(this.backpackId, this.num);
			// 将数量重新设置上来
			this.num = num;
		}
		let flag = this.root.backdrop.getGoodAndPut(this);
		if(flag){
			this.internalDestory();
		}
	}

	// 仅在世界中可获取
	getControlGetCondition() {
		return this.getIsGet() && this.backpackId == null;
	}

	getControlDestory(fn) {
		let flag = false;
		// 如果玩家的气血大于物品的重量的三倍，将可以直接销毁
		// 不过先判断这样物品是否可以直接销毁
		if (this.getIsDestory()) {
			if(fn){
				flag = fn();
			} else {
				let mainPlayer = this.root.mainPlayer.data;
				let goodData = this.getAttr();
				let weight = goodData[attrDefine.weight.key];
	
				// 直接销毁
				if (mainPlayer.attr.blood >= weight * 3) {
					flag = true;
				}
			}
		} else {
			this.root.gameState.dialogs.showWarning('警告', '该物品不可销毁')
			return;
		}

		if(!flag){
			this.root.gameState.dialogs.showWarning('提示', '你的实力不足以销毁这个物品');
			return;
		}

		if(this.backpackId != null){
			if(this.num > 1){
				this.internalConsumeGoodNum('销毁').then(num => {
					let flag = this.root.backdrop.consume(this.backpackId, num);

					if(flag){
						this.root.message('alert', `<${this.getTitle()}>x${num}被销毁`)
					}
				})
			} else{
				this.root.backdrop.consume(this.backpackId, 1);
				this.root.message('alert', `<${this.getTitle()}>已销毁`)
			}
		} 
		else {
			this.internalDestory();
			this.root.message('alert', `<${this.getTitle()}>已销毁`)
		}
	}

	getControlDiscard(){
		if(this.num > 1){
			this.internalConsumeGoodNum('丢弃').then(num => {
				let flag = this.root.backdrop.discard(this.backpackId, num);
				if(flag){
					this.root.message('alert', `<${this.getTitle()}>x${num}已丢弃`)
				}
			})
		} else {
			this.root.backdrop.discard(this.backpackId, 1);
			this.root.message('alert', `<${this.getTitle()}>已丢弃`)
		}
	}

	getControlDiscardCondition(){
		return this.backpackId != null;
	}

	// getControlSold(){
	// 	let price = this.getPrice();
	// 	price.price *= .6;

	// 	if(this.num > 1){
	// 		this.internalConsumeGoodNum('出售').then(num => {
	// 			let flag = this.root.backdrop.consume(this.backpackId, num);
	// 			if(flag){
	// 				this.root.backdrop.playerGetMoney(price.type, price.price * num);
	// 				this.root.message('alert', `<${this.getTitle()}>x${num}已出售`)
	// 			}
	// 		})
	// 	} else {
	// 		this.root.backdrop.consume(this.backpackId, 1);
	// 		this.root.backdrop.playerGetMoney(price.type, price.price);
	// 		this.root.message('alert', `<${this.getTitle()}>已出售`)
	// 	}
	// }

	// getControlSoldCondition(){
	// 	let currentNpc = this.root.worldMap.getCurrentNpcs();
		
	// 	for(let i = 0; i < currentNpc.length; i++){
	// 		if(currentNpc[i].getIsShop() && this.backpackId != null) return true;
	// 	}

	// 	return false;
	// }
}